package mall.dao;

import mall.model.User;
import mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-29 22:47
 *
 * */
public class UserDaoImpl implements UserDao {
    @Override
    public List<User> allUser() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<User> query = null;
        try {
            query = queryRunner.query("select * from user", new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void deleteUser(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("delete from user where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<User> searchUser(String word) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<User> query = null;
        try {
            query = queryRunner.query("select * from user where nickname like ?",
                    new BeanListHandler<User>(User.class),
                    "%" + word + "%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
